﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace BlueMarble.Genesis.Infrastructure.Background.Dynamic
{
    /// <summary>
    /// Interaction logic for ShapeLayer.xaml
    /// </summary>
    public partial class ShapeLayer : Canvas, INotifyPropertyChanged
    {
        protected const int ScreenWidth = 1920;
        protected const int ScreenHeight = 1080;

        public ShapeLayer()
        {
            InitializeComponent();
        }

        private ShapeBase CreateShape(Random rnd, double Scale, double Opacity)
        {
            ShapeBase shape = (ShapeBase)this.Shape.GetType().InvokeMember(null,
                System.Reflection.BindingFlags.DeclaredOnly |
                System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic |
                System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.CreateInstance, null, null, null);

            shape.SetValue(Canvas.LeftProperty, (double)rnd.Next(0, ScreenWidth));
            shape.SetValue(Canvas.TopProperty, (double)rnd.Next(0, ScreenHeight));
            shape.RenderTransformations(Scale, (double)rnd.Next(0, 360), Opacity);

            return shape;
        }

        public void PopulateShapes(int Number)
        {
            this.Children.Clear();

            Random rnd = new Random();

            for (int i = 0; i < Number; i++)
            {
                this.Children.Add(CreateShape(rnd, this.Scale, this.Opacity));
            }
        }

        public double Scale
        {
            get { return (double)GetValue(ScaleProperty); }
            set { SetValue(ScaleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Scale.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ScaleProperty =
            DependencyProperty.Register("Scale", typeof(double), typeof(ShapeLayer), new PropertyMetadata(1.0));

        public ShapeBase Shape
        {
            get { return (ShapeBase)GetValue(ShapeProperty); }
            set { SetValue(ShapeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Shape.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ShapeProperty =
            DependencyProperty.Register("Shape", typeof(ShapeBase), typeof(ShapeLayer), new PropertyMetadata(null));

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler Handler = PropertyChanged;
            if (Handler != null) Handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
